<template>
    <div class="order_item">
        <!-- <mt-cell :title="'订单编号: ' + i.order_no">提货码:{{i.code}}</mt-cell> -->
        <div class="address-info">
            <p class="order-address">{{i.dormitory_name}}-{{i.floor_name}}-{{i.consignee_address}}<span class="code">{{i.code}}</span></p>
            <mt-cell :title="i.consignee_name+'/'+i.consignee_phone">{{getNowFormatDate(i.create_time)}}</mt-cell>
        </div>
        <div class="address-info goods-order">
            <div>
                <p class="order-goods">{{i.children[0].name}}等{{i.children.length}}件商品</p>
                <p class="order-no"><span>{{i.order_no}}</span><i> | </i><span @click="copy(i.order_no)">复制</span></p>
            </div>
            <router-link :to="{path:'/orderInfo',query:{order_no:i.order_no}}"><i class="iconfont icon-gengduo"></i></router-link>
        </div>
        <div v-for="(a,index) of i.children" :key="index" class="order-good">
            <div class="goods-info">
                <p>{{a.name}}</p>
                <p v-if="a.type == 2" class="goods-details">{{a.meat_text}},{{a.veg_text}}</p>
            </div>
            <span>×{{a.goods_num}}</span>
            <span>{{a.goods_price}}</span>
        </div>
        <!-- <p class="order_detail order_user">下单时间:{{getNowFormatDate(i.create_time)}}</p>
        <p class="order_detail order_user">下单用户姓名:{{i.consignee_name}}</p>
        <p class="order_detail order_user">下单用户电话:{{i.consignee_phone}}</p>
        <p class="order_detail order_user">下单用户地址:{{i.school_name}}-{{i.dormitory_name}}-{{i.consignee_address}}</p> -->
        <!-- <p class="order_detail order_user">下单用户备注:{{!i.remark?"无备注":i.remark}}</p> -->
        <mt-cell title="下单用户备注:"></mt-cell>
        <p class="remark">{{!i.remark?"无备注":i.remark}}</p>
        <!-- <p class="order_detail order_user" v-show="i.send_name">接单配送员:{{i.send_name}}</p>
        <p class="/order/statusGetList" v-if="i.status == -1">退款原因:{{i.back_reason}}</p> -->
        <!-- <p class="order_detail">合计:{{i.total_price}}</p> -->
        <p class="order_detail earn">预计收入:<span>￥{{i.store_price}}元</span></p>
        <div class="btn_group">
            <mt-button class="btn" @click="ReceiveOrder(i.id)" v-if="!i.status">接单</mt-button>
            <mt-button class="btn" @click="Refund(i.order_no,i.goods_price)" v-if="i.status == -1">同意</mt-button>
            <mt-button class="btn" @click="Refuse(i.order_no)" v-if="i.status == -1">拒绝</mt-button>
            <a :href="'tel:'+i.consignee_phone"><mt-button class="btn">联系用户</mt-button></a>
            <a :href="'tel:'+i.send_phone" v-show="i.send_phone"><mt-button class="btn">联系骑手</mt-button></a>
            <mt-button class="btn" @click="reprint(i.order_no)">重新打印</mt-button>
        </div>
        <mt-popup
        v-model="popupVisible">
            <p class="printer-title">选择打印机</p>
            <section class="login_message">
                <input type="text" placeholder="请选择打印机编号" v-model="printerSn" readonly @click="printerVisible=!printerVisible">
                <div v-if="printerVisible" class="printer-list">
                    <p v-for="(item,index) of printerList" :key="index" @click="()=>{printerSn = item.sn;printerVisible=!printerVisible}">SN:{{item.sn}}</p>
                </div>
            </section>
            <!-- <select class="printer-select" v-model="printerSn">
                <option v-for="(item,index) of printerList" :key="index" :value ="item.sn" size="3">{{item.sn}}</option>
            </select> -->
            <mt-button class="btn" @click="selectPrinter">确定</mt-button>
        </mt-popup>
    </div>
</template>
<script>
import {receiveOrder,refund,refuse,printerList,againPrint} from '../api'
import {Toast,MessageBox} from 'mint-ui'
export default {
    name:"storeOrderList",
    props:["i"],
    data() {
        return {
            popupVisible:false,
            printerList:[],
            printerSn:'',
            order:'',
            printerVisible:false
        }
    },
    methods:{
        reprint(order){
            // const data={
            //     sn,
            //     order,
            //     token:this.$store.getters.token
            // }
            this.order = order;
            this.popupVisible = !this.popupVisible;
            const data = {
                token:this.$store.getters.token
            }
            printerList(data).then(res=>{
                console.log(res);
                this.printerList = res.data;
            })
        },
        selectPrinter(){
            console.log(this.printerSn);
            if(!this.printerSn){
                Toast('请选择打印机');
                return;
            }else if(!this.order){
                Toast('请选择订单');
                return;
            }
            const data={
                sn:this.printerSn,
                order:this.order,
                token:this.$store.getters.token
            }
            againPrint(data).then(res=>{
                Toast(res.msg);
                this.popupVisible = !this.popupVisible;
            })
        },
        getNowFormatDate(time) {//获取当前时间
        　　var date = new Date(time*1000);
        　　var seperator1 = "-";
        　　var seperator2 = ":";
        　　var month = date.getMonth() + 1<10? "0"+(date.getMonth() + 1):date.getMonth() + 1;
        　　var strDate = date.getDate()<10? "0" + date.getDate():date.getDate();
        　　var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        　　　　+ " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
        　　return currentdate;
        },
        Refuse(order_no){
            MessageBox({inputType:"text"})
            MessageBox.prompt("请输入拒绝退款原因").then(({ value, action }) => {
                if(action == 'confirm'){
                    if(!value){
                        Toast('请输入拒绝退款原因')
                        return;
                    }
                    console.log()
                    const data = {
                        token:this.$store.getters.token,
                        order_no,
                        unback_reason:value
                    }
                    refuse(data).then(res=>{
                        // console.log(res);
                        Toast(res.msg);
                    })
                }
            })
        },
        Refund(order_no,goods_price){
            MessageBox({inputType:"number"})
            MessageBox.prompt("请输入退款金额").then(({value,action}) => {
                if(action == "confirm"){
                    const data = {
                        token:this.$store.getters.token,
                        order_no,
                        type:2,
                        money:parseFloat(value)
                    }
                    if(goods_price<value){
                        Toast("退款总金额不能超过订单总金额")
                        return;
                    }else if(!value){
                        Toast("请输入退款金额")
                        return;
                    }
                    refund(data).then(res=>{
                        Toast(res.msg);
                    })
                }
            });
        },
        ReceiveOrder(id){
            const data = {
                token:this.$store.getters.token,
                id:parseInt(id)
            };
            receiveOrder(data).then(res=>{
                console.log(res);
                Toast(res.msg)
                if(res.code==200){
                    this.$router.go(0);
                }
            })
        },
        copy(c){
            // console.log(c);
            let url = c;
            let oInput = document.createElement('input');
            oInput.style.display = "none";
            oInput.value = url;
            document.body.appendChild(oInput);
            oInput.select(); // 选择对象;
            document.execCommand("Copy"); // 执行浏览器复制命令
            Toast('复制订单号码成功');
        }
    }
}
</script>
<style scoped>
    .printer-select{
        width: 80%;
        height: 2rem;
    }
    .mint-popup{
        width: 60%;
        padding: 1rem;
        text-align: center;
        border-radius: 1rem;
    }
    .order_item{
        border-bottom: 1px solid #ccc;
        padding:0 1rem;
        margin: 1rem 0;
        background: #fff;
    }
    .printer-title{
        margin: 1rem 0;
    }
    .order_item::before{
        content: "";
        display: table;
    }
    .order_detail{
        text-align: right;
        margin-right: 0.6rem;
        line-height: 1.4rem;
        font-size: 1.2rem;
        font-weight: bolder;
    }
    .remark{
        word-wrap: break-word;
        width: 80%;
        margin: auto;
    }
    .code{
        color: #F23D3D;
        position: absolute;
        right: 1.2rem;
        line-height: 2rem;
    }
    .address-info{
        padding-top: 0.5rem;
    }
    .goods-order{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 0.5rem;
        border-bottom: 1px solid #D8D8D8;
    }
    .goods-order .iconfont{
        color: #F0463C;
        font-size: 1.2rem;
    }
    .order-address{
        font-size: 1.2rem;
        font-weight: bolder;
        padding: 0 0.3rem;
    }
    .order-goods{
        font-size: 1rem;
        font-weight: bolder;
    }
    .order-good{
        width: 80%;
        margin: 1rem 0;
        margin-left: 20%;
        line-height: 2rem;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    .order-good span{
        width: 33%;
        text-align: right;
    }
    .order-good span:first-child{
        text-align: left;
    }
    .order-no{
        font-size: 0.8rem;
        color: #676767;
    }
    .btn_group{
        width: 94%;
        text-align: right;
        margin: 0 3%;
        border-top: 0.02rem solid #d8d8d8;
    }
    .btn{
        width: 6rem;
        height: 2rem;
        background: #fff;
        color: #333;
        border:0.04rem solid #333;
    }
    .btn-1{
        color: #F0463C;
        border-color: #F0463C;
    }
    .order_user{
        font-size: 0.8rem;
        text-align: left;
        margin-left: 0.3rem;
    }
    .earn{
        font-size: 1rem;
        font-weight: bolder;
        margin-bottom: 0.5rem;
    }
    .earn span{
        color: #C20C0C
    }
    .mint-cell-wrapper{
        font-size: 1rem;
    }
    .printer-list{
        position: absolute;
        background: #fff;
        width: 90%;
        margin: auto;
        z-index: 1000;
        margin:0 5%;
        border-bottom: 1px solid #ccc;
    }
    .printer-list p{
        border: 1px solid #ccc;
        line-height: 2rem;
        border-bottom: none;
    }
    .goods-details{
        font-size: 0.5rem;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
        line-height: 0.6rem;
    }
    .goods-info{
        width: 30%;
    }
</style>